package com.example.demosecurityjwt.pojo;

import com.example.demosecurityjwt.pojo.entity.TbUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: bysun
 * @Date: 2019/3/28 18:20
 */
public final class JwtUserFactory {

    private JwtUserFactory() {
    }

    public static JUser create(UserDto user) {
        return new JUser(
                user.getId(),
                user.getUsername(),
                user.getPassword(),
                mapToGrantedAuthorities(user.getRoles())
        );
    }

    private static List<GrantedAuthority> mapToGrantedAuthorities(List<String> authorities) {
        return authorities.stream()
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toList());
    }
}
